package com.nymph.session;

import com.nymph.binding.MapperRegistry;
import com.nymph.mapping.MappedStatement;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 */
public class Configuration {

    public static Properties PROPS=new Properties();

    // mapper代理注册器
    protected final MapperRegistry mapperRegistry=new MapperRegistry();

    // mapper文件的select/update语句的id和SQL语句属性
    protected final Map<String, MappedStatement> mappedStatements=new HashMap<>();

    public <T> void addMapper(Class<T> type){
        this.mapperRegistry.addMapper(type);
    }

    /**
     * getMapper
     *
     * @param type
     * @param sqlSession
     * @return
     * @see
     */
    public <T> T getMapper(Class<T> type, SqlSession sqlSession)
    {
        return this.mapperRegistry.getMapper(type, sqlSession);
    }

    /**
     * addMappedStatement
     *
     * @param key
     * @param mappedStatement
     */
    public void addMappedStatement(String key, MappedStatement mappedStatement)
    {
        this.mappedStatements.put(key, mappedStatement);
    }

    /**
     * 获取mappedstatement
     * @param id
     * @return
     */
    public MappedStatement getMappedStatement(String id){
        return this.mappedStatements.get(id);
    }

    /**
     * 获取字符型属性
     */
    public static String getProperty(String key){
        return getProperty(key,"");
    }

    // 默认空值
    public static String getProperty(String key,String defaultValue){
        return PROPS.containsKey(key)?PROPS.getProperty(key):defaultValue;
    }
}
